// pages/meeting/apply/apply.js
var api = require('../../../utils/httpUtil.js');
var dateUtils = require('../../../utils/dateUtils.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    index: '',
    topic: '',
    date: '2024-01-01',
    startTime: '08:00',
    endTime: '09:00',
    meetingRooms: [],
    openId: app.globalData.openId,
    meetingName: '',
    meetingId: '',
    coupons: [{
      id: 1,
      coupon: 'A'
    }, {
      id: 2,
      coupon: 'B'
    }],
    couponId: null,
    coupon: null,
    showCancel: false,
    meetingInfos: [],
    dateList: [],
    tabCur: 0,
    schedule: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      date: dateUtils.currentDate()
    })
    this.loadMeetingRoomData();
    this.loadCouponsData();
    this.initDate();
    this.initSchedule();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  //初始化日期选择
  initDate() {
    var today = dateUtils.currentDate();
    var dateList = [{
      id: 0,
      day: dateUtils.getDay(today),
      date: '今天',
      dateTime: today
    }];
    for (var i = 1; i < 14; i++) {
      dateList.push({
        id: i,
        day: dateUtils.getDay(dateUtils.addDays(today, i)),
        date: dateUtils.getWeekday(dateUtils.addDays(today, i)),
        dateTime: dateUtils.addDays(today, i)
      });
    }
    this.setData({
      dateList: dateList
    })
  },

  //初始化会议选择列表
  initSchedule() {
    var scheduleList = [];
    for (var i = 0; i < 24; i++) {
      scheduleList.push({
        id: i,
        name: dateUtils.formatNumber(i) + ":00",
        originSel: {
          top: false,
          bottom: false
        },
        currentSel: {
          top: false,
          bottom: false
        }
      });
    }
    this.setData({
      schedule: scheduleList
    })
  },
  //加载会议室信息
  loadMeetingRoomData() {
    api.httpRequest(api.meetingRoomListAPI, {})
      .then(res => {
        if (res.code == 200) {
          this.setData({
            meetingRooms: res.data
          });
        } else {
          wx.showToast({
            title: res.msg,
            duration: 2000,
            mask: true,
            icon: 'none'
          })
        }
      })
      .catch(error => {
        wx.showToast({
          title: error.message,
          duration: 1000,
          mask: true,
          icon: 'none'
        })
      });
  },
  //加载优惠券信息
  loadCouponsData() {
    api.httpRequest(api.couponsAPI + "/" + this.data.openId, {})
      .then(res => {
        if (res.code == 200) {
          this.setData({
            coupons: res.data
          });
        } else {
          wx.showToast({
            title: res.msg,
            duration: 2000,
            mask: true,
            icon: 'none'
          })
        }
      })
      .catch(error => {
        wx.showToast({
          title: error.message,
          duration: 1000,
          mask: true,
          icon: 'none'
        })
      });
  },
  topicInput: function (e) {
    this.setData({
      topic: e.detail.value
    })
  },
  dateChange(e) {
    this.setData({
      date: e.detail.value
    })
    this.loadMeetingInfo();
  },

  //查询会议室的占用情况
  loadMeetingInfo() {
    api.httpRequest(api.meetingInfoAPI, {
        meetingId: this.data.meetingId,
        applyDate: this.data.date
      })
      .then(res => {
        if (res.code == 200) {
          this.initMeetingInfo(res.data);
        } else {
          wx.showToast({
            title: res.msg,
            duration: 2000,
            mask: true,
            icon: 'none'
          })
        }
      })
      .catch(error => {
        wx.showToast({
          title: error.message,
          duration: 1000,
          mask: true,
          icon: 'none'
        })
      });
  },
  initMeetingInfo(meetingList) {
    var scheduleList = this.data.schedule;
    scheduleList.forEach(item => {
        item.originSel.top=false;
        item.originSel.bottom=false;
    });
    this.setData({
      schedule: scheduleList
    })

    for (var i = 0; i < meetingList.length; i++) {
      var mInfo = meetingList[i];
      var startTime = mInfo.applyStartTime.substring(11, 16);
      var endTime = mInfo.applyEndTime.substring(11, 16);
      var scheduleList = this.data.schedule;
      for (var j = 0; j < scheduleList.length; j++) {
        var schTime = scheduleList[j].name;
        if (schTime >= startTime && schTime < endTime) {
          scheduleList[j].originSel.top = true;
        }
        if (schTime >= startTime && schTime < endTime) {
          scheduleList[j].originSel.bottom = true;
        }
      }
      this.setData({
        schedule: scheduleList
      })
    }
  },
  // startChange(e) {
  //   this.setData({
  //     startTime: e.detail.value
  //   })
  // },
  // endChange(e) {
  //   this.setData({
  //     endTime: e.detail.value
  //   })
  // },
  startPickerChange: function (e) {
    this.setData({
      multiStart: e.detail.value
    })
    this.setData({
      startTime: this.data.multiArray[0][this.data.multiStart[0]] + ":" + this.data.multiArray[1][this.data.multiStart[1]]
    })
  },
  endPickerChange: function (e) {
    this.setData({
      multiEnd: e.detail.value
    })
    this.setData({
      endTime: this.data.multiArray[0][this.data.multiEnd[0]] + ":" + this.data.multiArray[1][this.data.multiEnd[1]]
    })
  },


  roomChange: function (e) {
    this.setData({
      index: e.detail.value,
      meetingId: this.data.meetingRooms[e.detail.value].id,
      meetingName: this.data.meetingRooms[e.detail.value].name
    });
  },
  couponChange: function (e) {
    this.setData({
      index1: e.detail.value,
      couponId: this.data.coupons[e.detail.value].id,
      coupon: this.data.coupons[e.detail.value].coupon
    });
  },
  confirmClick() {
    this.setData({
      showCancel: false
    })
  },
  tabSelect(e) {
    var dateTime = this.data.dateList.filter(item=>item.id==e.currentTarget.dataset.id);
    this.setData({
      date: dateUtils.formatDate(dateTime[0].dateTime)
    });
    this.loadMeetingInfo();
    this.setData({
      tabCur: e.currentTarget.dataset.id,
    })
  },
  scheduleSelectA(e) {
    var id = e.currentTarget.dataset.id;
    var scheduleList = this.data.schedule;
    var items = scheduleList.filter(item => item.id === id);
    var item = items.length === 1 ? items[0] : items
    if (item.originSel.top != true) {
      item.currentSel.top = !item.currentSel.top;
    }
    this.setData({
      schedule: scheduleList
    });
  },
  scheduleSelectB(e) {
    var id = e.currentTarget.dataset.id;
    var scheduleList = this.data.schedule;
    var items = scheduleList.filter(item => item.id === id);
    var item = items.length === 1 ? items[0] : items
    if (item.originSel.bottom != true) {
      item.currentSel.bottom = !item.currentSel.bottom;
    }
    this.setData({
      schedule: scheduleList
    });
  },
  submit() {
    if (!this.data.topic) {
      wx.showToast({
        title: '会议主题不能为空',
        duration: 1000,
        mask: true,
        icon: 'none'
      })
      return;
    }
    if (!this.data.meetingId) {
      wx.showToast({
        title: '会议地点不能为空',
        duration: 1000,
        mask: true,
        icon: 'none'
      })
      return;
    }
    const stTime = this.data.date + " " + this.data.startTime + ":00";
    const edTime = this.data.date + " " + this.data.endTime + ":00";
    api.httpRequest(api.meetingApplyAPI, {
        meetingId: this.data.meetingId,
        meetingName: this.data.meetingName,
        topic: this.data.topic,
        applyStartTime: stTime,
        applyEndTime: edTime,
        openId: this.data.openId,
        couponId: this.data.couponId
      })
      .then(res => {
        wx.showToast({
          title: res.msg,
          duration: 1000,
          mask: true,
          icon: 'none',
          success: function () {
            if (res.code == 200) {
              setTimeout(function () {
                wx.navigateBack();
              }, 1000);
            }
          }
        })
      })
      .catch(error => {
        wx.showToast({
          title: error.message,
          duration: 1000,
          mask: true,
          icon: 'none'
        })
      });
  },
  pageClick() {
    if (this.data.showCancel == true) {
      var scheduleList = this.data.schedule;
      var selList = scheduleList.filter(item => item.currentSel.top == true || item.currentSel.bottom == true);
      console.log(selList)
      if (selList.length > 0) {
        var startTime = null;
        var endTime = null;
        for (var i = 0; i < selList.length; i++) {
          var item = selList[i];
          if (item.currentSel.top == true) {
            var sTime = dateUtils.formatNumber(item.id) + ":00";
            var eTime = dateUtils.formatNumber(item.id) + ":30";
            if (startTime == null || startTime >= sTime) {
              startTime = sTime;
            }
            if (endTime == null || endTime <= eTime) {
              endTime = eTime;
            }
          }
          if (item.currentSel.bottom == true) {
            var sTime = dateUtils.formatNumber(item.id) + ":30";
            var eTime = dateUtils.formatNumber(item.id + 1) + ":00";
            if (startTime == null || startTime >= sTime) {
              startTime = sTime;
            }
            if (endTime == null || endTime <= eTime) {
              endTime = eTime;
            }
          }
        }
        this.setData({
          startTime: startTime,
          endTime: endTime
        })
      }
    }
    this.setData({
      showCancel: false
    })
  },
  test() {

  },
  showSchedule() {
    if (!this.data.meetingId) {
      return;
    };
    var dateList=this.data.dateList;
    console.log(dateList);
    dateList.forEach(item => {
        if(item.dateTime.substring(0,10)==this.data.date)
        {
          this.setData({
            tabCur: item.id
          })
        }
    });
    this.loadMeetingInfo();
    this.setData({
      showCancel: true
    })
  }
})